package com.dianrui.qiyouriding.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.adapter.MyOrderAdapter;
import com.dianrui.qiyouriding.bean.OrderBean;
import com.dianrui.qiyouriding.util.JsonUtils;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的订单
 *
 * @author lzy
 */
public class MyOrderActivity extends BaseActivity {
    private int pageIndex = 1;
    private List<OrderBean> list = new ArrayList<>();
    private MyOrderAdapter myOrderAdapter;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @Override
    public int getContentViewId() {
        return R.layout.activity_my_order;
    }

    @Override
    public void initView() {
        customInit(true, R.color.main_color);
        title.setText(getString(R.string.my_txt1));
        LinearLayoutManager layoutManager = new LinearLayoutManager(MyOrderActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);
        myOrderAdapter = new MyOrderAdapter(R.layout.order_list_item, list, MyOrderActivity.this);
        recyclerview.setAdapter(myOrderAdapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getMyOrder(true);
                refreshLayout.finishRefresh();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageIndex++;
                getMyOrder(false);
                refreshLayout.finishLoadMore();
            }
        });
        myOrderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                OrderBean orderBean = (OrderBean) adapter.getItem(position);
                if (orderBean != null) {
                    switch (orderBean.status) {
                        case "1":
                            Intent intent = new Intent(MyOrderActivity.this, RidingActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("number", orderBean.number);
                            bundle.putString("vehicle_id", orderBean.vehicle_id);
                            bundle.putString("vehicle_status", "1");
                            intent.putExtras(bundle);
                            startActivity(intent);
                            break;
                        case "2":
                            Intent pauseintent = new Intent(MyOrderActivity.this, RidingActivity.class);
                            Bundle pausebundle = new Bundle();
                            pausebundle.putString("number", orderBean.number);
                            pausebundle.putString("vehicle_id", orderBean.vehicle_id);
                            pausebundle.putString("vehicle_status", "2");
                            pauseintent.putExtras(pausebundle);
                            startActivity(pauseintent);
                            break;

                        case "3":
                            Intent paymentintent = new Intent(MyOrderActivity.this, RidingPriceActivity.class);
                            Bundle paymentbundle = new Bundle();
                            paymentbundle.putString("number", orderBean.number);
                            paymentbundle.putString("time", orderBean.end_time);
                            paymentbundle.putString("id", orderBean.order_id);
                            paymentintent.putExtras(paymentbundle);
                            startActivity(paymentintent);
                            break;

                        case "4":
                            Intent orderdetailintent = new Intent(MyOrderActivity.this, OrderDetailActivity.class);
                            Bundle orderdetailibundle = new Bundle();
                            orderdetailibundle.putString("order_id", orderBean.order_id);
                            orderdetailintent.putExtras(orderdetailibundle);
                            startActivity(orderdetailintent);
                            break;

                        default:
                            break;
                    }
                }
            }
        });

    }

    @Override
    public void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMyOrder(true);
    }




    private void getMyOrder(final boolean isRfresh) {
        if (isRfresh) {
            pageIndex = 1;
        }
        JsonObject orderList = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            orderList.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        orderList.addProperty("p", pageIndex);
        OkHttpRequest.getInstance().postRequests(Url.ORDER_LIST, orderList.toString(), new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (isRfresh) {
                    list.clear();
                }
                if (!StringUtils.isEmpty(result)) {
                    try {
                        if (isFinishing()) {
                            return;
                        }
                        String status = JsonUtils.getJsonStr(result, Constant.STATUS);
                        if (Constant.DATASUCCESS.equals(status)) {
                            String data = JsonUtils.getJsonStr(result, "data");
                            if (!StringUtils.isEmpty(data)) {
                                List<OrderBean> orderlist = JsonUtils.jsonStrToList(data, new TypeToken<ArrayList<OrderBean>>() {}.getType());
                                if (orderlist != null && orderlist.size() > 0) {
                                    list.addAll(orderlist);
                                    if (myOrderAdapter != null) {
                                        myOrderAdapter.setNewData(list);
                                        refreshLayout.finishRefresh();
                                    }
                                }
                            }
                        }else if (Constant.BACKLOGIN.equals(status)) {
                            SPUtils.getInstance().clear();
                            JumpKillActivitys(LoginActivity.class);
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (list.size() <= 0) {
                        myOrderAdapter.getData().clear();
                        myOrderAdapter.setEmptyView(LayoutInflater.from(MyOrderActivity.this).inflate(R.layout.empty_view,null));
                        myOrderAdapter.notifyDataSetChanged();
                    } else {
                        recyclerview.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                refreshLayout.setNoMoreData(false);
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });

    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (list != null) {
            list.clear();
            list = null;
        }
    }
}
